package com.app.micaihu.custom.view.dataview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.micaihu.R;
import com.app.micaihu.bean.topic.PostBean;
import com.app.micaihu.custom.view.CustomImageView;
import com.app.utils.util.ViewSizeUtils;
import com.app.utils.util.image.LoadImageUtils;
import com.app.utils.util.view.expression.view.BiaoQinTextView;

/**
 * 话题评论的控件(帖子)
 * Created by linkai on 2016/9/9.
 */
public class TopicCommentView extends LinearLayout {

    private CustomImageView headimg;
    private TextView name;
    private TextView time;
    private BiaoQinTextView content;
    private LinearLayout picLayout;
    private CustomImageView img1;
    private CustomImageView img2;
    private CustomImageView img3;
    private OnClickListener onClickListener;

    private PostBean postBean;//显示的帖子


    public TopicCommentView(Context context,OnClickListener onClickListener) {
        super(context);
        this.onClickListener = onClickListener;
        initView();
    }

    public TopicCommentView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    private void initView() {
        inflate(getContext(), R.layout.view_topic_comment, this);

        headimg = (CustomImageView) findViewById(R.id.civ_headpic);
        name = (TextView) findViewById(R.id.tv_nickname);
        time = (TextView) findViewById(R.id.tv_publishtime);
        content = (BiaoQinTextView) findViewById(R.id.tv_postcontent);
        picLayout = (LinearLayout) findViewById(R.id.ll_image_container);
    }

    /**
     * 设置数据
     *
     * @param postBean
     */
    public void setData(PostBean postBean) {
        if (postBean == null) {
            return;
        }
        this.postBean = postBean;
        setOnClickListener(onClickListener);
        if (headimg == null) {
            headimg = (CustomImageView) findViewById(R.id.civ_headpic);
        }
        LoadImageUtils.getInstance().loadCircleImage(headimg, postBean.getHeadPic());
        if (name == null) {
            name = (TextView) findViewById(R.id.tv_nickname);
        }
        name.setText(postBean.getNickName());
        if (time == null) {
            time = (TextView) findViewById(R.id.tv_publishtime);
        }
        time.setText(postBean.getPublishTime());
        if (content == null) {
            content = (BiaoQinTextView) findViewById(R.id.tv_postcontent);
        }
        content.setPicText(postBean.getContent());
        if (picLayout == null) {
            picLayout = (LinearLayout) findViewById(R.id.ll_image_container);
        }
        if (img1 == null) {
            img1 = (CustomImageView) findViewById(R.id.img1);
            img1.setOnClickListener(this.onClickListener);
            int thumbSize = (int) ViewSizeUtils.changeView30Per(null, 7.5f, 29f);
            ViewSizeUtils.changeViewWH(img1, thumbSize, thumbSize);
        }
        if (img2 == null) {
            img2 = (CustomImageView) findViewById(R.id.img2);
            img2.setOnClickListener(this.onClickListener);
            int thumbSize = (int) ViewSizeUtils.changeView30Per(null, 7.5f, 29f);
            ViewSizeUtils.changeViewWH(img2, thumbSize, thumbSize);
        }
        if (img3 == null) {
            img3 = (CustomImageView) findViewById(R.id.img3);
            img3.setOnClickListener(this.onClickListener);
            int thumbSize = (int) ViewSizeUtils.changeView30Per(null, 7.5f, 29f);
            ViewSizeUtils.changeViewWH(img3, thumbSize, thumbSize);
        }

        //加载图片
        if (postBean.getImgList() != null && postBean.getImgList().size() > 0) {
            if (postBean.getImgList().size() == 1) {
                LoadImageUtils.getInstance().loadImage(img1, postBean.getImgList().get(0));
                img1.setVisibility(View.VISIBLE);
                img1.setTag(R.id.tag_first, postBean.getImgList());
                img1.setTag(R.id.tag_secong,0);
                img2.setVisibility(View.GONE);
                img3.setVisibility(View.GONE);
            } else if (postBean.getImgList().size() == 2) {
                LoadImageUtils.getInstance().loadImage(img1, postBean.getImgList().get(0));
                LoadImageUtils.getInstance().loadImage(img2, postBean.getImgList().get(1));
                img1.setVisibility(View.VISIBLE);
                img2.setVisibility(View.VISIBLE);
                img1.setTag(R.id.tag_first, postBean.getImgList());
                img1.setTag(R.id.tag_secong, 0);
                img2.setTag(R.id.tag_first,postBean.getImgList());
                img2.setTag(R.id.tag_secong,1);
                img3.setVisibility(View.GONE);
            } else if (postBean.getImgList().size() > 2) {
                LoadImageUtils.getInstance().loadImage(img1, postBean.getImgList().get(0));
                LoadImageUtils.getInstance().loadImage(img2, postBean.getImgList().get(1));
                LoadImageUtils.getInstance().loadImage(img3, postBean.getImgList().get(2));
                img1.setVisibility(View.VISIBLE);
                img2.setVisibility(View.VISIBLE);
                img3.setVisibility(View.VISIBLE);
                img1.setTag(R.id.tag_first, postBean.getImgList());
                img1.setTag(R.id.tag_secong, 0);
                img2.setTag(R.id.tag_first,postBean.getImgList());
                img2.setTag(R.id.tag_secong,1);
                img3.setTag(R.id.tag_first,postBean.getImgList());
                img3.setTag(R.id.tag_secong,2);
            }
            content.setMaxLines(2);
            picLayout.setVisibility(View.VISIBLE);
        } else {
            content.setMaxLines(1);
            picLayout.setVisibility(View.GONE);
        }
    }

    public PostBean getPostBean(){
        return postBean;
    }

}
